package com.photographysharing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.photographysharing.common.ThreadLocalOperation;
import com.photographysharing.dto.FollowDTO;
import com.photographysharing.entity.Fans;
import com.photographysharing.entity.Follows;
import com.photographysharing.response.DatasResponse;
import com.photographysharing.response.MessageResponse;
import com.photographysharing.response.SignResponse;
import com.photographysharing.service.FansService;
import com.photographysharing.service.FollowsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/follow")
public class FollowsController {
    private FollowsService followsService;
    private FansService fansService;

    public FollowsController(FollowsService followsService, FansService fansService) {
        this.followsService = followsService;
        this.fansService = fansService;
    }


    @GetMapping("/isFollow")
    public boolean isFollow(String followId){
        String userId = ThreadLocalOperation.getThreadLocalUserId();
        Follows follows = followsService.getOne(new QueryWrapper<Follows>().eq("user_id",userId).eq("follow_id",followId));
        return follows==null?false:true;
    }

    @GetMapping("/follow")
    public MessageResponse follow(String followId){
        String userId = ThreadLocalOperation.getThreadLocalUserId();
        boolean isFollow = isFollow(followId);
        if(isFollow){
            if(followsService.remove(new QueryWrapper<Follows>().eq("user_id",userId).eq("follow_id",followId)))
            {
                fansService.remove(new QueryWrapper<Fans>().eq("user_id",followId).eq("fans_id",userId));
                return new MessageResponse("取消成功!",false);
            }

        }
        Follows follows  = new Follows(userId,followId);
        Fans fans = new Fans(followId,userId);
        if(followsService.save(follows)){
            fansService.save(fans);
            return new MessageResponse("关注成功!");
        }
        return new MessageResponse("关注失败!",false);
    }

    @GetMapping("/follows")
    public DatasResponse<FollowDTO> follows(String userId){
        return followsService.getFollows(userId);
    }



}
